package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/8 20:40
 */
@Controller
public class UploadController {
    @PostMapping("upload")
    public String upload(@RequestParam("photo") MultipartFile file, HttpServletRequest request, HttpServletResponse response)  {
        String originalFilename = file.getOriginalFilename();     //获取源文件名
        String pictype = originalFilename.substring(originalFilename.lastIndexOf("."));     //图片类型 后缀
        try {
            file.transferTo(new File("C:\\Knight\\pic\\person\\" + request.getParameter("userid")+pictype));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        return "redirect:home";
    }
}
